﻿using Admin.NET.Core.Service;
using Admin.NET.Application.Const;
using Admin.NET.Application.Entity;
using Microsoft.AspNetCore.Http;
namespace Admin.NET.Application;
/// <summary>
/// 设备清单服务
/// </summary>
[ApiDescriptionSettings(ApplicationConst.GroupName, Order = 100)]
public class BoardDeviceService : IDynamicApiController, ITransient
{
    private readonly SqlSugarRepository<BoardDevice> _rep;
    public BoardDeviceService(SqlSugarRepository<BoardDevice> rep)
    {
        _rep = rep;
    }

    /// <summary>
    /// 分页查询设备清单
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Page")]
    public async Task<SqlSugarPagedList<BoardDeviceOutput>> Page(BoardDeviceInput input)
    {
        var query = _rep.AsQueryable()
            .WhereIF(!string.IsNullOrWhiteSpace(input.SearchKey), u =>
                u.DeviceNo.Contains(input.SearchKey.Trim())
                || u.DeviceName.Contains(input.SearchKey.Trim())
                || u.Demo.Contains(input.SearchKey.Trim())
            )
            .WhereIF(!string.IsNullOrWhiteSpace(input.DeviceNo), u => u.DeviceNo.Contains(input.DeviceNo.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.DeviceName), u => u.DeviceName.Contains(input.DeviceName.Trim()))
            .WhereIF(input.DevicePlace>0, u => u.DevicePlace == input.DevicePlace)
            .WhereIF(input.DeviceState>0, u => u.DeviceState == input.DeviceState)
            .WhereIF(!string.IsNullOrWhiteSpace(input.Demo), u => u.Demo.Contains(input.Demo.Trim()))
            //处理外键和TreeSelector相关字段的连接
            //.LeftJoin<BoardPlace>((u, deviceplace) => u.DevicePlace == deviceplace.Id )
            .OrderBy(u => u.CreateTime)
            //.Select((u, deviceplace) => new BoardDeviceOutput
            .Select(u => new BoardDeviceOutput
            {
                Id = u.Id,
                DeviceNo = u.DeviceNo,
                DeviceName = u.DeviceName,
                DevicePlace = u.DevicePlace, 
                DevicePlacePlaceName = u.Place.PlaceName,
                DeviceState = u.DeviceState,
                Demo = u.Demo,
                CreateOrgId = u.CreateOrgId,
                CreateOrgName = u.CreateOrgName,
                CreateTime = u.CreateTime,
                UpdateTime = u.UpdateTime,
                CreateUserId = u.CreateUserId,
                CreateUserName = u.CreateUserName,
                UpdateUserId = u.UpdateUserId,
                UpdateUserName = u.UpdateUserName,
                IsDelete = u.IsDelete,
            });
        return await query.OrderBuilder(input).ToPagedListAsync(input.Page, input.PageSize);
    }

    /// <summary>
    /// 增加设备清单
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Add")]
    public async Task<long> Add(AddBoardDeviceInput input)
    {
        var entity = input.Adapt<BoardDevice>();
        await _rep.InsertAsync(entity);
        return entity.Id;
    }

    /// <summary>
    /// 删除设备清单
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Delete")]
    public async Task Delete(DeleteBoardDeviceInput input)
    {
        var entity = await _rep.GetFirstAsync(u => u.Id == input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
        await _rep.FakeDeleteAsync(entity);   //假删除
        //await _rep.DeleteAsync(entity);   //真删除
    }

    /// <summary>
    /// 更新设备清单
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Update")]
    public async Task Update(UpdateBoardDeviceInput input)
    {
        var entity = input.Adapt<BoardDevice>();
        await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
    }

    /// <summary>
    /// 获取设备清单
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "Detail")]
    public async Task<BoardDevice> Detail([FromQuery] QueryByIdBoardDeviceInput input)
    {
        return await _rep.GetFirstAsync(u => u.Id == input.Id);
    }

    /// <summary>
    /// 获取设备清单列表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "List")]
    public async Task<List<BoardDeviceOutput>> List([FromQuery] BoardDeviceInput input)
    {
        return await _rep.AsQueryable().Select<BoardDeviceOutput>().ToListAsync();
    }

    /// <summary>
    /// 获取所在车间列表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [ApiDescriptionSettings(Name = "BoardPlaceDevicePlaceDropdown"), HttpGet]
    public async Task<dynamic> BoardPlaceDevicePlaceDropdown()
    {
        return await _rep.Context.Queryable<BoardPlace>()
                .Select(u => new
                {
                    Label = u.PlaceName,
                    Value = u.Id
                }
                ).ToListAsync();
    }




}

